package com.wqtz.chechai.view.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.CarBookViewPageAdapter;
import com.wqtz.chechai.presenter.ImageBrowsePresenter;
import com.wqtz.chechai.view.impl.ImageBrowseViewImpl;
import com.wqtz.chechai.widget.ViewPagerFixed;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2017/2/10 10:29
 */

public class CarDrivingBooksViewActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, ImageBrowseViewImpl {
	@BindView(R.id.viewPager)
	ViewPagerFixed vp;
	@BindView(R.id.hint)
	TextView hint;
	@BindView(R.id.save)
	TextView save;
	private CarBookViewPageAdapter adapter;
	private ImageBrowsePresenter pre;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setFullScreen();
		setContentView(R.layout.activity_car_book_view);
		ButterKnife.bind(this);
		initPresenter();
		pre.loadImage();
	}

	private void setFullScreen() {
		/*set it to be no title*/
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	    /*set it to be full screen*/
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}


	public void initPresenter() {
		pre = new ImageBrowsePresenter(this);
	}

	@Override
	public Intent getDataIntent() {
		return getIntent();
	}

	@Override
	public Context getMyContext() {
		return this;
	}

	@Override
	public void setImageBrowse(List<String> images, int position) {
		if (adapter == null && images != null && images.size() != 0) {
			adapter = new CarBookViewPageAdapter(this, images);
			vp.setAdapter(adapter);
			vp.setCurrentItem(position);
			vp.addOnPageChangeListener(this);
			hint.setText(position + 1 + "/" + images.size());
		}
	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

	}

	@Override
	public void onPageSelected(int position) {
		pre.setPosition(position);
		hint.setText(position + 1 + "/" + pre.getImages().size());
	}

	@Override
	public void onPageScrollStateChanged(int state) {

	}


	public static void startActivity(Context context, ArrayList<String> images, int position) {
		Intent intent = new Intent(context, CarDrivingBooksViewActivity.class);
		intent.putStringArrayListExtra("images", images);
		intent.putExtra("position", position);
		context.startActivity(intent);
	}

	@OnClick(R.id.save)
	public void onClick() {
		Logger.e("开始加载图片");
		pre.saveImage();
	}


	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public Activity getActivity() {
		return null;
	}

	@Override
	public void showError(String message, int code) {

	}

}
